package v1

import (
	"RoccBlog/global"
	"RoccBlog/model/req_resp"
	"RoccBlog/service"
	"RoccBlog/util"
	gormadapter "github.com/casbin/gorm-adapter/v3"
	"github.com/gin-gonic/gin"
)

type CaSbinService struct {
	service.BaseOperationService
	service.BaseService
}

func (s *CaSbinService) Add(data req_resp.CaSbinReq) error {
	global.CaSbin.AddPolicy(data.Sub, data.Obj, data.Act)
	return global.CaSbin.SavePolicy()
}

func (s *CaSbinService) Remove(data req_resp.CaSbinReq) error {
	global.CaSbin.RemovePolicy(data.Sub, data.Obj, data.Act)
	return global.CaSbin.SavePolicy()
}

func (s *CaSbinService) Find(ctx *gin.Context, data any) (any, error) {
	size, num := s.GetPage(ctx)
	db := global.DB.Model(&gormadapter.CasbinRule{})

	var list []req_resp.CaSbinReq
	var count int64
	db.Count(&count)
	err := db.Offset(size * num).Limit(size).Find(&list).Error
	return util.ToPageInfo(count, size, num, list), err
}
